/**
 * @author LIU YUAN
 * @date May 15, 2011 11:12:21 AM
 * @param <T>
 */
#include "StdAfx.h"
#include "Asm.h"

using namespace upupc;
Asm::Asm()
	:bufferSize(0)
{
}


Asm::~Asm(void)
{
}

void
Asm::append(
        Instruction *ins)
{
	list.push_back(ins);
	bufferSize += ins->size();
}

DWORD
Asm::size()
{
	return bufferSize;
}

CHAR*
Asm::parse(DWORD eip)
{
	if(bufferSize==0){
		return NULL;
	}

	PCHAR buffer = new CHAR[bufferSize];
	DWORD pos = 0;
	
	InstructionList::iterator it =  list.begin();
	InstructionList::iterator end =  list.end();
	for(;it!=end;it++){
		if(pos>=bufferSize)
		{
			break;
		}

		Instruction* ins = (*it);
		DWORD m = ins->size();
		if(m==0)
		{
			continue;
		}

		CHAR* content = ins->parse(eip);
		eip = eip + m;

		memcpy_s(buffer + pos,bufferSize-pos,content,m);
		pos += m;

		delete content;
	}

	return buffer;
}